package com.douqu.game.core.config.goods;

import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.entity.common.TwoTuple;
import com.douqu.game.core.entity.ext.BagInfo;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.Arrays;

/**
 * @author: Gavin.
 * Description:
 * Date: 2017/11/1 0001 下午 7:28
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class AccessoryUpConfig extends GameObject {

    public String effect;

    /**资源*/
    public GoodsData[] needProps;

    /**返还的道具，资源，装备*/
    public GoodsData[] restitution;

    /**解锁等级*/
    public int unlockLevel;

    @Override
    public boolean check() {
        if(needProps == null)
        {
            LogUtils.error("AccessoryUp check needProps is null -> id:" + id + " name:" + name);
            return false;
        }

        if(restitution == null)
        {
            LogUtils.error("AccessoryUp check restitution is null -> id:" + id + " name:" + name);
            return false;
        }
        return true;
    }

    /**
     * 检查背包资源是否足够
     *
     * */
    public TwoTuple<Boolean, Integer> checkEnoughProp(BagInfo bagInfo, int quality) {
        for (int i =0 ; i  < needProps.length; i++){
            //品质
            if(quality == needProps[i].type){
                if(bagInfo.getPropCount(needProps[i].id) >= needProps[i].value)
                    return new TwoTuple<>(true, 0);
                else
                    return new TwoTuple<>(false, needProps[i].id);
            }
        }
        return new TwoTuple<>(true, 0);
    }



    @Override
    public boolean setVariable(String key, String value) {
        if("needProps".equals(key)){
            needProps = LoadUtils.loadGoodDataToArray(key, value);
            return LoadUtils.checkProp(needProps, this.getClass(), key, id);
        }
        else if("restitution".equals(key))
        {
            restitution = LoadUtils.loadGoodDataToArray(key, value);
            GoodsData[] checkData = new GoodsData[restitution.length];
            for(int i = 0; i < restitution.length; i ++){
                checkData[i] = new GoodsData(SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_PROPS_VALUE,
                        restitution[i].id, restitution[i].value);
            }
            return LoadUtils.checkGoods(checkData, this.getClass(), key, id);
        }else{
            return super.setVariable(key, value);
        }
    }

    @Override
    public String toString() {
        return "AccessoryUp{" +
                ", needProps=" + needProps +
                ", restitution=" + Arrays.toString(restitution) +
                '}';
    }
}
